<?php

namespace gzapi\common\business;
use gzapi\common\model\GzHiseServiceProTrainModel;
use gzapi\common\model\GzHiseServiceProjectModel;
use gzapi\common\model\GzHiseServiceProTrainDetailModel;
use gzapi\common\model\RlEtAzsModel;

class Train
{
    protected $GzHiseServiceTrain;
    protected $GzHiseServiceProject;
    protected $GzHiseServiceProTrainDetail;
    protected $RlEtAzs;
    protected $training_status = [
        '创建' => 1,
        '编辑' => 2,
        '提交' => 3,
    ];
    public function __construct()
    {
        $this->RlEtAzs = new RlEtAzsModel;
        $this->GzHiseServiceTrain = new GzHiseServiceProTrainModel;
        $this->GzHiseServiceProject = new GzHiseServiceProjectModel;
        $this->GzHiseServiceProTrainDetail = new GzHiseServiceProTrainDetailModel;
    }

    //获取安装商信息
    public function trainGetFws($where)
    {
        $data = $this->RlEtAzs->trainGetFws($where);
        return $data;
    }


    //获取培训列表
    public function trainList($where)
    {
        $data = $this->GzHiseServiceTrain->trainList($where);
        return $data;
    }

    //创建培训单
    public function trainAdd($where,$data)
    {
        $result = ['code'=>1,'msg'=>'创建失败'];
        //查询安装单是否是分公司的，安装单状态是否可以创建培训
        $azd = $this->GzHiseServiceProject->getOne($where);
        if(!$azd)
        {
            $result['code'] = 0;
            $result['msg'] = '安装单不存在';
            return $result;
        }
        //如果工程状态不是未开工和安装中，不可以创建
        if($azd['gczt'] != 6 && $azd['gczt'] != 7)
        {
            $result['code'] = 0;
            $result['msg'] = '安装单不能创建培训单';
            return $result;
        }
        //创建培训单
        $result['code'] = $this->GzHiseServiceTrain->trainAdd($data);
        //修改项目主表培训状态
        if($result['code'])
        {
            $this->GzHiseServiceProject->where('azd_id',$data['azd_id'])->update(['start_training'=>1,'gczt'=>7]);
        }

        return $result;
    }

    //获取培训详情
    public function trainDetails($where)
    {
        $data = $this->GzHiseServiceTrain->trainDetails($where);
        $data['sign'] = $this->GzHiseServiceProTrainDetail->where(['train_no'=>$data['training_no']])->select();
        return $data;
    }

    //暂存培训单
    public function trainSave($where,$data)
    {
        $result = ['code'=>1,'msg'=>'保存失败'];
        //查询安装单是否是分公司的，安装单状态是否可以创建培训
        $pxd = $this->GzHiseServiceTrain->trainDetails($where);
        if(!$pxd)
        {
            $result['code'] = 0;
            $result['msg'] = '培训单不存在';
            return $result;
        }
        //如果工程状态是3不可以创建
        if($pxd['training_status'] == 3)
        {
            $result['code'] = 0;
            $result['msg'] = '培训单已提交';
            return $result;
        }
        //暂存培训单
        $result['code'] = $this->GzHiseServiceTrain->trainSave($where,$data);
        return $result;
    }

    //保存用户信息
    public function azdSign($where,$data)
    {
        $result = ['code'=>1,'msg'=>'创建失败'];
        //查询安装单是否是分公司的，安装单状态是否可以创建培训
        $detail = $this->GzHiseServiceProTrainDetail->getSing($where);
        if($detail)
        {
            $result['code'] = 0;
            $result['msg'] = '已经进行签到，不能再次提交';
            return $result;
        }
        //创建培训单
        $result['code'] = $this->GzHiseServiceProTrainDetail->trainSingAdd($data);
        return $result;
    }

    //判断是否可以签到
    public function isSign($where)
    {
        $data = $this->GzHiseServiceTrain->trainDetails($where);
        if(!$data || $this->training_status['提交'] == $data['training_status'] || $data['training_time'] > date('Y-m-d',time()))
        {
            return 1;
        }
        return 0;
    }
}